package com.anis.project.medcare.save.report;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.anis.jpa.medical.model.Common;
import com.anis.jpa.medical.model.MedicalReport;
import com.anis.jpa.medical.model.Patient;
import com.anis.project.medcare.Activator;
import com.anis.project.medcare.dicom.viewer.view.SWT2Dutil;
import com.anis.project.medcare.search.table.providers.SearchPatientContentProv;
import com.anis.project.medcare.search.table.providers.SearchPatientLabelProv;

public class SaveReportDialog extends Dialog {
	private Text patientNameText;
	private Text patientPhooneText;
	private Text patientLstNameText;
	private TableViewer table;
	protected boolean retrieveAll;
	private Patient selectedPatient;
	private MedicalReport radioReportToSave;
	private String reportName;
	private String reportDescription;
	private Text reportNameText;
	private Text reportDescriptionText;
	private Text patientIDText;
	private ArrayList<ImageData> imageDataList;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public SaveReportDialog(MedicalReport radioReport,
			ArrayList<ImageData> list, Shell parentShell) {
		super(parentShell);
		this.radioReportToSave = radioReport;
		this.imageDataList = list;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(5, false));
		
				final Button btnRetrieveAll = new Button(container, SWT.CHECK);
				btnRetrieveAll.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						if (btnRetrieveAll.getSelection()) {
							patientNameText.setEnabled(false);
							patientLstNameText.setEnabled(false);
							patientPhooneText.setEnabled(false);
							patientIDText.setEnabled(false);
							retrieveAll = true;
						} else {
							patientNameText.setEnabled(true);
							patientLstNameText.setEnabled(true);
							patientPhooneText.setEnabled(true);
							patientIDText.setEnabled(true);
							retrieveAll = false;
						}
					}
				});
				btnRetrieveAll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
						false, 1, 1));
				btnRetrieveAll.setText("Retrieve All");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Label lblPatientName = new Label(container, SWT.NONE);
		lblPatientName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));
		lblPatientName.setText("Patient Name :");

		patientNameText = new Text(container, SWT.BORDER);
		patientNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 2, 1));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Label lblPatientLastName = new Label(container, SWT.NONE);
		lblPatientLastName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				false, false, 1, 1));
		lblPatientLastName.setText("Patient Last Name :");

		patientLstNameText = new Text(container, SWT.BORDER);
		patientLstNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				false, false, 2, 1));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Label lblPhone = new Label(container, SWT.NONE);
		lblPhone.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
				1, 1));
		lblPhone.setText("Phone :");

		patientPhooneText = new Text(container, SWT.BORDER);
		GridData gd_patientPhooneText = new GridData(SWT.FILL, SWT.CENTER,
				false, false, 1, 1);
		gd_patientPhooneText.widthHint = 125;
		patientPhooneText.setLayoutData(gd_patientPhooneText);

		Label lblPatientId = new Label(container, SWT.NONE);
		GridData gd_lblPatientId = new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1);
		gd_lblPatientId.widthHint = 122;
		lblPatientId.setLayoutData(gd_lblPatientId);
		lblPatientId.setText("Patient ID :");

		patientIDText = new Text(container, SWT.BORDER);
		patientIDText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		new Label(container, SWT.NONE);

		Label lblReportName = new Label(container, SWT.NONE);
		lblReportName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));
		lblReportName.setText("Report Name :");

		reportNameText = new Text(container, SWT.BORDER);
		reportNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				reportName = reportNameText.getText();
				if (!reportName.isEmpty())
					getOKButton().setEnabled(true);
				else
					getOKButton().setEnabled(false);
			}
		});
		reportNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));

		Label lblReportDescription = new Label(container, SWT.NONE);
		lblReportDescription.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				false, false, 1, 1));
		lblReportDescription.setText("Report Description :");

		reportDescriptionText = new Text(container, SWT.BORDER);
		reportDescriptionText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				reportDescription = reportDescriptionText.getText();
			}
		});
		reportDescriptionText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				false, false, 1, 1));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Button btnNewButton = new Button(container, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				retrieveInputOfTable(retrieveAll, table);
			}
		});
		btnNewButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnNewButton.setText("Look up");
		new Label(container, SWT.NONE);

		table = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
		final Table table_1 = table.getTable();
		table_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem selectedItem = table_1.getSelection()[0];
				if (selectedItem.getData() instanceof Patient) {
					selectedPatient = (Patient) selectedItem.getData();

				}
			}
		});
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		table_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 5,
				4));

		TableViewerColumn tableViewerColumn = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientId = tableViewerColumn.getColumn();
		tblclmnPatientId.setWidth(100);
		tblclmnPatientId.setText("Patient ID");

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientName = tableViewerColumn_1.getColumn();
		tblclmnPatientName.setWidth(109);
		tblclmnPatientName.setText("Patient Name");

		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientLastName = tableViewerColumn_2.getColumn();
		tblclmnPatientLastName.setWidth(122);
		tblclmnPatientLastName.setText("Patient Last Name");

		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(table,
				SWT.NONE);
		TableColumn tblclmnPatientPhone = tableViewerColumn_3.getColumn();
		tblclmnPatientPhone.setWidth(100);
		tblclmnPatientPhone.setText("Patient Phone");
		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		getOKButton().setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(546, 360);
	}

	private void retrieveInputOfTable(boolean retrieveAll, TableViewer table) {
		List<Patient> patientList;
		if (!retrieveAll) {
			String patientName = patientNameText.getText();
			String patientLastName = patientLstNameText.getText();
			Integer patientPhone = null;
			Integer patientID = null;
			if (!patientPhooneText.getText().isEmpty())
				patientPhone = Integer.valueOf(patientPhooneText.getText());
			if (!patientIDText.getText().isEmpty())
				patientID = Integer.valueOf(patientIDText.getText());
			if (patientNameText.getText().isEmpty())
				patientName = null;
			if (patientLstNameText.getText().isEmpty())
				patientLastName = null;

			patientList = Activator
					.getDefault()
					.databaseBinder()
					.retrievePatient(patientName, patientLastName,
							patientPhone, patientID);
		} else {
			patientList = Activator.getDefault().databaseBinder()
					.retrieveAllPatient();
		}
		table.setLabelProvider(new SearchPatientLabelProv());
		table.setContentProvider(new SearchPatientContentProv());
		table.setInput(patientList);
	}

	@Override
	protected void okPressed() {
		super.okPressed();
		Date date = new Date();
		if (radioReportToSave != null) {
			radioReportToSave.setReportName(reportName);
			radioReportToSave.setReportDescription(reportDescription);
			radioReportToSave.setIdradioreport(Long.valueOf(selectedPatient
					.getIdpatient()) + date.getSeconds());
			selectedPatient.addRadioreport(radioReportToSave);
		} else if (imageDataList != null) {
			radioReportToSave = new MedicalReport();
			radioReportToSave.setReportName(reportName);
			radioReportToSave.setReportDescription(reportDescription);
			radioReportToSave.setReportType(Common.RADREPTYPE);
			radioReportToSave.setIdradioreport(Long.valueOf(selectedPatient
					.getIdpatient()) + date.getSeconds());
			radioReportToSave.setReportcontent(createByteArray(imageDataList));
			selectedPatient.addRadioreport(radioReportToSave);
			radioReportToSave.setReportdate(date);

		}

		Activator.getDefault().databaseBinder()
				.savePatientReport(selectedPatient, radioReportToSave);

	}

	public byte[] createByteArray(ArrayList<ImageData> obj) {
		for (int i = 0; i < obj.size(); i++) {
			ImageData img = obj.get(i);
			BufferedImage buffuredImage = SWT2Dutil.convertToAWT(obj.get(i));
			byte[] bytes = null;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			try {
				ObjectOutputStream oos = new ObjectOutputStream(bos);
				oos.writeObject(buffuredImage);
				oos.flush();
				oos.close();
				bos.close();
				bytes = bos.toByteArray();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		byte[] bytes = null;
		return bytes;
	}

}
